package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.DateFormatUtil;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.dao.SuperviseQuestionDao;
import com.dream.inspect.modules.business.entity.SuperviseQuestionEntity;
import com.dream.inspect.modules.business.service.SuperviseQuestionService;


@Service("superviseQuestionService")
public class SuperviseQuestionServiceImpl extends ServiceImpl<SuperviseQuestionDao, SuperviseQuestionEntity> implements SuperviseQuestionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseQuestionEntity> page = this.selectPage(
                new Query<SuperviseQuestionEntity>(params).getPage(),
                new EntityWrapper<SuperviseQuestionEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<SuperviseQuestionEntity> findSuperviseQuestionList(Map<String, String> params) {
		int currPage = Integer.valueOf(params.get("currPage"));
    	int pageSize  = Integer.valueOf(params.get("pageSize"));
    	String userDeptName = params.get("userDeptName");
    	currPage = (currPage - 1) * pageSize;
		List<SuperviseQuestionEntity> sqrList = new ArrayList<SuperviseQuestionEntity>();
		//组装条件
		EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
		sqrList = baseMapper.findSuperviseQuestionList(wrapper, currPage, pageSize, userDeptName);
		return sqrList;
	}

	@Override
	public int findSuperviseQuestionListCount(Map<String, String> params) {
	    String userDeptName = params.get("userDeptName");
		//组装条件
		EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
		int countNum = baseMapper.findSuperviseQuestionListCount(wrapper, userDeptName);
		return countNum;
	}
	

    @Override
    public List<SuperviseQuestionEntity> findSuperviseQuestionListForCount(Map<String, String> params) {
        int currPage = Integer.valueOf(params.get("currPage"));
        int pageSize  = Integer.valueOf(params.get("pageSize"));
        String userDeptName = params.get("userDeptName");
        currPage = (currPage - 1) * pageSize;
        List<SuperviseQuestionEntity> sqrList = new ArrayList<SuperviseQuestionEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionListForCount(wrapper, currPage, pageSize, userDeptName);
        return sqrList;
    }

    @Override
    public int findSuperviseQuestionListForCountNum(Map<String, String> params) {
        String userDeptName = params.get("userDeptName");
        //组装条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
        int countNum = baseMapper.findSuperviseQuestionListForCountNum(wrapper, userDeptName);
        return countNum;
    }
    
    @Override
    public List<SuperviseQuestionEntity> findSuperviseQuestionDetailList(Map<String, String> params) {
        String userDeptName = params.get("userDeptName");
        List<SuperviseQuestionEntity> sqrList = new ArrayList<SuperviseQuestionEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionDetailList(wrapper, userDeptName);
        return sqrList;
    }
    

	@Override
	public int findSuperviseQuestionForModifyStatusNum(Map<String, String> params) {

        String dcRecordId = params.get("dcRecordId");  //督察记录id
        EntityWrapper<SuperviseQuestionEntity> wrapper = new EntityWrapper<SuperviseQuestionEntity>();
		wrapper.setEntity(new SuperviseQuestionEntity());
        //督察记录ID
		if (StringUtils.isNotEmpty(dcRecordId)) {
			wrapper.eq("DC_RECORD_ID", dcRecordId);
		}
        int countNum = baseMapper.findSuperviseQuestionForModifyStatusNum(wrapper);
        return countNum;
	}
	

	@Override
	public List<SuperviseQuestionEntity> findSuperviseQuestionListNoImage(Map<String, String> params) {
		int currPage = Integer.valueOf(params.get("currPage"));
        int pageSize  = Integer.valueOf(params.get("pageSize"));
        String modelId = params.get("modelId");
        currPage = (currPage - 1) * pageSize;
        List<SuperviseQuestionEntity> sqrList = new ArrayList<SuperviseQuestionEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionListNoImage(wrapper, currPage, pageSize, modelId);
        return sqrList;
	}

	@Override
	public int findSuperviseQuestionListNoImageCount(Map<String, String> params) {
        String modelId = params.get("modelId");
        //组装条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);
        int countNum = baseMapper.findSuperviseQuestionListNoImageCount(wrapper, modelId);
        return countNum;
	}
	
	@Override
	public int selectThisMonthCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);

        int countNum = baseMapper.selectThisMonthCount(wrapper);
        return countNum;
	}

	@Override
	public SuperviseQuestionEntity findSuperviseQuestionForSearchPage(Map<String, String> params) {
		String id = params.get("id");
        return baseMapper.findSuperviseQuestionForSearchPage(id);
	}

	@Override
	public int findSuperviseQuestionForSearchPageCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseQuestionEntity> wrapper = addCondition(params);

        int countNum = baseMapper.findSuperviseQuestionForSearchPageCount(wrapper);
        return countNum;
	}
	
	@Override
	public PageUtils selectSuperviseQuestionListPage(Map<String, String> params) {
		String currP = params.get("currPage");
		int currPage = 1;
		if (StringUtils.isNotEmpty(currP)) {
			currPage = Integer.valueOf(currP);
		}
		String pageS = params.get("pageSize");
		int pageSize  = 10;
		if (StringUtils.isNotEmpty(pageS)) {
			pageSize  = Integer.valueOf(pageS);
		}
		
		Page<SuperviseQuestionEntity> page = new Page<>(currPage, pageSize);
		SuperviseQuestionEntity sqEntity = new SuperviseQuestionEntity();
		String detpTemp = params.get("deptId");
		if (StringUtils.isNotEmpty(detpTemp)) {
			Long deptId = Long.valueOf(detpTemp);
			sqEntity.setDeptId(deptId);
		}
		if (StringUtils.isNotEmpty(params.get("v2"))) {
			sqEntity.setV2(params.get("v2"));
		}
		String findKey = params.get("findKey");
		if (StringUtils.isNotEmpty(findKey)) {
			sqEntity.setV1(findKey);
		}
		page.setRecords(this.baseMapper.selectSuperviseQuestionListPage(page, sqEntity));
        return new PageUtils(page);
	}
	
	@Override
	public List<SuperviseQuestionEntity> selectQuestionDetailInfo(Map<String, String> params) {
		SuperviseQuestionEntity sqEntity = new SuperviseQuestionEntity();
		String questionModifyId = params.get("questionModifyId");
		if (StringUtils.isNotEmpty(questionModifyId)) {
			sqEntity.setModifyId(Long.valueOf(questionModifyId));
		}
		String dcRecordId = params.get("dcRecordId");//记录ID
		if (StringUtils.isNotEmpty(dcRecordId)) {
			sqEntity.setDcRecordId(Long.valueOf(dcRecordId));
		}
		String questionId = params.get("questionId");//问题ID
		if (StringUtils.isNotEmpty(questionId)) {
			sqEntity.setId(Long.valueOf(questionId));
		}
		String modifyStatus = params.get("modifyStatus");//整改状态(0：待整改，1：无整改，2：已整改，3：备存无整改，4：备存待整改)
		if (StringUtils.isNotEmpty(modifyStatus)) {
			sqEntity.setModifyStatus(modifyStatus);
		}
		String checkStatus = params.get("checkStatus");//审核状态(0：待审核，1：审核通过，2：审核未通过)
		if (StringUtils.isNotEmpty(checkStatus)) {
			sqEntity.setV1(checkStatus);
		}
		
		List<SuperviseQuestionEntity> resultList = this.baseMapper.selectQuestionDetailInfo(sqEntity);
		
		return resultList;
	}
	
	/**
	 * 拼接查询条件
	 * 
	 * @param params
	 * @return
	 */
	private EntityWrapper<SuperviseQuestionEntity> addCondition(Map<String, String> params) {
	    String questionId = params.get("questionId");
		String dcRecordId = params.get("dcRecordId");
        String projectId = params.get("projectId");//专项ID
        String sortByTime = params.get("sortByTime");//时间排序
        String modifyStatus = params.get("modifyStatus");//整改状态
        String dcfs = params.get("dcfs");//督察记录方式(1：实时拍录，2：专项督察，3：分级督察)
        String userDeptName = params.get("userDeptName");
        String thisMonthFlg = params.get("thisMonthFlg");//本月标志
        String searchText = params.get("searchText");
		
		EntityWrapper<SuperviseQuestionEntity> wrapper = new EntityWrapper<SuperviseQuestionEntity>();
		wrapper.setEntity(new SuperviseQuestionEntity());
	    //问题ID
        if (StringUtils.isNotEmpty(questionId)) {
            wrapper.eq("sq.ID", questionId);
        }
		//督察记录ID
		if (StringUtils.isNotEmpty(dcRecordId)) {
			wrapper.eq("DC_RECORD_ID", dcRecordId);
		}
        //专项ID
        if (StringUtils.isNotEmpty(projectId)) {
            wrapper.eq("sr.PROJECT_ID", projectId);
        }
        //记录方式
        if (StringUtils.isNotEmpty(dcfs)) {
            wrapper.eq("sr.DCFS", dcfs);
        }
        //整改状态(0：待整改，1：未整改，2：已整改)
        if (StringUtils.isNotEmpty(modifyStatus)) {
            wrapper.eq("sq.MODIFY_STATUS", modifyStatus);
        }
        wrapper.eq("sq.DELETE_FLAG", "0");
        if (StringUtils.isNotEmpty(thisMonthFlg)) {
			if ("1".equals(thisMonthFlg)) {
				String dt = DateFormatUtil.getFirstDayOfMonthForString();
				wrapper.and(" Date(sq.CREATE_TIME) >= '" + dt + "'");
			}
		}
        //用户部门名称
        if (StringUtils.isNotEmpty(userDeptName)) {
            wrapper.andNew();
            wrapper.eq("sq.DCDW", userDeptName);
            wrapper.or("sq.TO_DCDW={0}", userDeptName);
        }
        
        //时间排序（1：升序，2：降序）
        if (StringUtils.isNotEmpty(sortByTime)) {
            if ("1".equals(sortByTime)) {
                wrapper.orderBy("DCSJ", true);//督察时间
            } else {
                wrapper.orderBy("DCSJ", false);
            }
        }
        //全文检索条件
		if (StringUtils.isNotEmpty(searchText)) {
			wrapper.andNew();
	  		wrapper.like("ss.QUESTION_DESCRIPTION", searchText);
	  		wrapper.or("sr.DCMC like {0}", "%" + searchText + "%");
		}
    
    	return wrapper;
	}

}
